<template>
  <!-- 税收管理--新增和编辑 -->
  <el-dialog :title="mode=='add'?'新增':'编辑'"
             class="addCommission"
             width='662px'
             :close-on-click-modal="false"
             :visible.sync="visible">
    <!-- 具体表单 -->
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             label-width="130px">
      <el-form-item label="姓名"
                    prop="roleName">
        <el-input v-model="dataForm.roleName"
                  placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="身份证号码"
                    prop="idCard">
        <el-input v-model="dataForm.idCard"
                  placeholder="身份证号码"></el-input>
      </el-form-item>
      <el-form-item label="手机号码"
                    prop="phone">
        <el-input v-model="dataForm.phone"
                  placeholder="手机号码"></el-input>
      </el-form-item>
      <el-form-item label="个人银行卡号信息"
                    prop="bankCard">
        <el-input v-model="dataForm.bankCard"
                  placeholder="个人银行卡号信息"></el-input>
      </el-form-item>
      <el-form-item label="用工的企业名称"
                    prop="userName">
        <el-input v-model="dataForm.userName"
                  placeholder="用工的企业名称"></el-input>
      </el-form-item>
      <el-form-item label="开票金额"
                    prop="money">
        <el-input v-model="dataForm.money"
                  placeholder="开票金额"></el-input>
      </el-form-item>
      <el-form-item label="佣金金额"
                    prop="brokMoney">
        <el-input v-model="dataForm.brokMoney"
                  placeholder="佣金金额"></el-input>
      </el-form-item>
      <el-form-item label="结算时间"
                    prop="endTime">
        <el-input v-model="dataForm.endTime"
                  placeholder="结算时间"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button type="primary"
                 class="btn"
                 v-if="mode == 'add'">确认新增</el-button>
      <el-button type="primary"
                 class="btn"
                 v-else>确认修改</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  // props: ['mode'],
  data () {
    return {
      mode: 'add',
      // 隐藏
      visible: false,
      // 数据
      menuList: [],
      menuListTreeProps: {
        label: 'name',
        children: 'children'
      },
      dataForm: {
        id: 0,
        roleName: '',
        remark: ''
      },
      dataRule: {
        roleName: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ]
      },
      options: [{
        value: '男',
        label: '男'
      }, {
        value: '女',
        label: '女'
      }],
      tempKey: -666666 // 临时key, 用于解决tree半选中状态项不能传给后台接口问题. # 待优化
    }
  },
  methods: {
    // 父调用的方法
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sys/role/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'roleId': this.dataForm.id || undefined,
              'roleName': this.dataForm.roleName,
              'remark': this.dataForm.remark,
              'menuIdList': [].concat(this.$refs.menuListTree.getCheckedKeys(), [this.tempKey], this.$refs.menuListTree.getHalfCheckedKeys())
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  // 成功之后的回调
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss">
.addCommission {
  .el-input.el-input--medium {
    width: 370px;
  }
  .el-dialog__header {
    background-color: #f8f8f8;
  }
  .el-dialog__body {
    display: flex;
    justify-content: center;
  }
  .btn {
    width: 370px;
  }
  .dialog-footer {
    text-align: center !important;
    padding-bottom: 39px;
  }
  .el-dialog__footer {
    text-align: center;
    padding-bottom: 39px;
  }
}
</style>
